home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / GETINTKT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-10  |  6.6 KB  |  293 lines

  1. /*
  2.  *
  3.  * $Source: /mit/kerberos/src/lib/krb/RCS/get_in_tkt.c,v $
  4.  * $Author: jtkohl $
  5.  *
  6.  * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  */
  11.  
  12. #ifndef lint
  13. static char rcsid_get_in_tkt_c[] =
  14. "$Header: get_in_tkt.c,v 4.12 89/07/18 16:32:56 jtkohl Exp $";
  15. #endif /* lint */
  16.  
  17. #include <mit_copy.h>
  18. #include <krb.h>
  19. #include <prot.h>
  20.  
  21. #ifndef NULL
  22. #define NULL 0
  23. #endif
  24.  
  25. /*
  26.  * This file contains two routines: passwd_to_key() converts
  27.  * a password into a DES key (prompting for the password if
  28.  * not supplied), and krb_get_pw_in_tkt() gets an initial ticket for
  29.  * a user.
  30.  */
  31.  
  32. /*
  33.  * passwd_to_key(): given a password, return a DES key.
  34.  * There are extra arguments here which (used to be?)
  35.  * used by srvtab_to_key().
  36.  *
  37.  * If the "passwd" argument is not null, generate a DES
  38.  * key from it, using string_to_key().
  39.  *
  40.  * If the "passwd" argument is null, call des_read_password()
  41.  * to prompt for a password and then convert it into a DES key.
  42.  *
  43.  * In either case, the resulting key is put in the "key" argument,
  44.  * and 0 is returned.
  45.  */
  46.  
  47. /*ARGSUSED */
  48. static int passwd_to_key(user,instance,realm,passwd,key)
  49.     char *user, *instance, *realm, *passwd;
  50.     C_Block key;
  51. {
  52. #ifdef NOENCRYPTION
  53.     if (!passwd)
  54.     placebo_read_password(key, "Password: ", 0);
  55. #else /* Do encyryption */
  56.     if (passwd)
  57.         string_to_key(passwd, key);
  58.     else {
  59.         des_read_password(key, "Password: ", 0);
  60.     }
  61. #endif /* NOENCRYPTION */
  62.     return (0);
  63. }
  64.  
  65. /*
  66.  * krb_get_pw_in_tkt() takes the name of the server for which the initial
  67.  * ticket is to be obtained, the name of the principal the ticket is
  68.  * for, the desired lifetime of the ticket, and the user's password.
  69.  * It passes its arguments on to krb_get_in_tkt(), which contacts
  70.  * Kerberos to get the ticket, decrypts it using the password provided,
  71.  * and stores it away for future use.
  72.  *
  73.  * krb_get_pw_in_tkt() passes two additional arguments to krb_get_in_tkt():
  74.  * the name of a routine (passwd_to_key()) to be used to get the
  75.  * password in case the "password" argument is null and NULL for the
  76.  * decryption procedure indicating that krb_get_in_tkt should use the 
  77.  * default method of decrypting the response from the KDC.
  78.  *
  79.  * The result of the call to krb_get_in_tkt() is returned.
  80.  */
  81.  
  82. krb_get_pw_in_tkt(user,instance,realm,service,sinstance,life,password)
  83.     char *user, *instance, *realm, *service, *sinstance;
  84.     int life;
  85.     char *password;
  86. {
  87.     return(krb_get_in_tkt(user,instance,realm,service,sinstance,life,
  88.                           passwd_to_key, (int(*))NULL, password));
  89. }
  90.  
  91. #ifdef NOENCRYPTION
  92. /*
  93.  * $Source: /mit/kerberos/src/lib/krb/RCS/get_in_tkt.c,v $
  94.  * $Author: jtkohl $
  95.  *
  96.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  97.  * of Technology.
  98.  *
  99.  * For copying and distribution information, please see the file
  100.  * <mit-copyright.h>.
  101.  *
  102.  * This routine prints the supplied string to standard
  103.  * output as a prompt, and reads a password string without
  104.  * echoing.
  105.  */
  106.  
  107. #ifndef    lint
  108. static char rcsid_read_password_c[] =
  109. "$Header: get_in_tkt.c,v 4.12 89/07/18 16:32:56 jtkohl Exp $";
  110. #endif    lint
  111.  
  112. #include <des.h>
  113. #include "conf.h"
  114.  
  115. #include <stdio.h>
  116. #ifdef    BSDUNIX
  117. #include <strings.h>
  118. #include <sys/ioctl.h>
  119. #include <signal.h>
  120. #include <setjmp.h>
  121. #else
  122. char     *strcpy();
  123. int      strcmp();
  124. #endif
  125.  
  126. #ifdef    BSDUNIX
  127. static jmp_buf env;
  128. #endif
  129.  
  130. #ifdef BSDUNIX
  131. static void sig_restore();
  132. static push_signals(), pop_signals();
  133. int placebo_read_pw_string();
  134. #endif
  135.  
  136. /*** Routines ****************************************************** */
  137. int
  138. placebo_read_password(k,prompt,verify)
  139.     des_cblock *k;
  140.     char *prompt;
  141.     int    verify;
  142. {
  143.     int ok;
  144.     char key_string[BUFSIZ];
  145.  
  146. #ifdef BSDUNIX
  147.     if (setjmp(env)) {
  148.     ok = -1;
  149.     goto lose;
  150.     }
  151. #endif
  152.  
  153.     ok = placebo_read_pw_string(key_string, BUFSIZ, prompt, verify);
  154.     if (ok == 0)
  155.     bzero(k, sizeof(C_Block));
  156.  
  157. lose:
  158.     bzero(key_string, sizeof (key_string));
  159.     return ok;
  160. }
  161.  
  162. /*
  163.  * This version just returns the string, doesn't map to key.
  164.  *
  165.  * Returns 0 on success, non-zero on failure.
  166.  */
  167.  
  168. int
  169. placebo_read_pw_string(s,max,prompt,verify)
  170.     char *s;
  171.     int    max;
  172.     char *prompt;
  173.     int    verify;
  174. {
  175.     int ok = 0;
  176.     char *ptr;
  177.     
  178. #ifdef BSDUNIX
  179.     jmp_buf old_env;
  180.     struct sgttyb tty_state;
  181. #endif
  182.     char key_string[BUFSIZ];
  183.  
  184.     if (max > BUFSIZ) {
  185.     return -1;
  186.     }
  187.  
  188. #ifdef    BSDUNIX
  189.     bcopy(old_env, env, sizeof(env));
  190.     if (setjmp(env))
  191.     goto lose;
  192.  
  193.     /* save terminal state*/
  194.     if (ioctl(0,TIOCGETP,&tty_state) == -1) 
  195.     return -1;
  196.  
  197.     push_signals();
  198.     /* Turn off echo */
  199.     tty_state.sg_flags &= ~ECHO;
  200.     if (ioctl(0,TIOCSETP,&tty_state) == -1)
  201.     return -1;
  202. #endif
  203.     while (!ok) {
  204.     printf(prompt);
  205.     fflush(stdout);
  206. #if 0    /* CROSSMSDOS */
  207.     h19line(s,sizeof(s),0);
  208.     if (!strlen(s))
  209.         continue;
  210. #else
  211.     if (!fgets(s, max, stdin)) {
  212.         clearerr(stdin);
  213.         continue;
  214.     }
  215.     if ((ptr = index(s, '\n')))
  216.         *ptr = '\0';
  217. #endif
  218.     if (verify) {
  219.         printf("\nVerifying, please re-enter %s",prompt);
  220.         fflush(stdout);
  221. #if 0  /* CROSSMSDOS */
  222.         h19line(key_string,sizeof(key_string),0);
  223.         if (!strlen(key_string))
  224.         continue;
  225. #else
  226.         if (!fgets(key_string, sizeof(key_string), stdin)) {
  227.         clearerr(stdin);
  228.         continue;
  229.         }
  230.             if ((ptr = index(key_string, '\n')))
  231.         *ptr = '\0';
  232. #endif
  233.         if (strcmp(s,key_string)) {
  234.         printf("\n\07\07Mismatch - try again\n");
  235.         fflush(stdout);
  236.         continue;
  237.         }
  238.     }
  239.     ok = 1;
  240.     }
  241.  
  242. #ifdef    BSDUNIX
  243. lose:
  244.     if (!ok)
  245.     bzero(s, max);
  246.     printf("\n");
  247.     /* turn echo back on */
  248.     tty_state.sg_flags |= ECHO;
  249.     if (ioctl(0,TIOCSETP,&tty_state))
  250.     ok = 0;
  251.     pop_signals();
  252.     bcopy(env, old_env, sizeof(env));
  253. #endif
  254.     if (verify)
  255.     bzero(key_string, sizeof (key_string));
  256.     s[max-1] = 0;        /* force termination */
  257.     return !ok;            /* return nonzero if not okay */
  258. }
  259.  
  260. #ifdef    BSDUNIX
  261. /*
  262.  * this can be static since we should never have more than
  263.  * one set saved....
  264.  */
  265. #ifdef POSIX
  266. static void (*old_sigfunc[NSIG])();
  267. #else
  268. static int (*old_sigfunc[NSIG])();
  269. #endif POSIX
  270.  
  271. static push_signals()
  272. {
  273.     register i;
  274.     for (i = 0; i < NSIG; i++)
  275.     old_sigfunc[i] = signal(i,sig_restore);
  276. }
  277.  
  278. static pop_signals()
  279. {
  280.     register i;
  281.     for (i = 0; i < NSIG; i++)
  282.     signal(i,old_sigfunc[i]);
  283. }
  284.  
  285. static void sig_restore(sig,code,scp)
  286.     int sig,code;
  287.     struct sigcontext *scp;
  288. {
  289.     longjmp(env,1);
  290. }
  291. #endif
  292. #endif /* NOENCRYPTION */
  293.